<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@include file="/view/common/js-css.jsp" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugin/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/plugin/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/plugin/zTree_v3-master/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/plugin/zTree_v3-master/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};
	var zTree;
	// 定义初始化对应权限的函数
	function initNode() {
		
		$.get("RoleServlet?command=getGrant&roleId=${param.roleId}",function(data) {
			 var json = eval("("+data+")");
			 for(var i=0;i<json.length;i++) {
				zTree.checkNode(zTree.getNodeByParam("id",json[i].id),true);
			}
		});
		
		zTree.expandAll(true); 
	} 
	
	$(document).ready(function() {
		$.get("RoleServlet?command=grant",function(data) {
			// 初始化树形菜单
			$.fn.zTree.init($("#rightTree"), setting, eval("("+data+")"));
			zTree = $.fn.zTree.getZTreeObj("rightTree")
			initNode();
		});
		
		// 定义授权按钮的点击事件
		$("#grant").bind("click",function() {
			// 定义rightId组成字符串
			var rightIds="";
			// 获取当前选择的节点集合
			var checkedNodes = zTree.getCheckedNodes();
			for(var i=0;i<checkedNodes.length;i++) {
				var node = checkedNodes[i];
				rightIds+=node.id+",";
			}
			rightIds = rightIds.substr(0,rightIds.length-1);
			var roleId = '${param.roleId}';
			location.href="RoleServlet?command=permisson&rightIds="+rightIds+"&roleId="+roleId;
		});
		
	});

</script>
</head>
<body style="background-image: url('static/common/img/bground3.jpg');">
	<div class="content_wrap">
		<div class="left">
			<ul id="rightTree" class="ztree"></ul>
		</div>
	</div>
	<div class="submit">
		<input type="button" id="grant" value=" 授  权 " /> 
	</div>
</body>
</html>